import React from 'react';

class BuggyComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = { counter: 0 };
        this.handleClick = this.handleClick.bind(this);
        this.decClick = this.decClick.bind(this);
    }

    handleClick() {
        this.setState(({ counter }) => ({
            counter: counter + 1
        }));
    }

    decClick() {
        this.setState(({ counter }) => ({
            counter: counter - 1
        }));
    }

    render() {
        // 故意引发错误
        if (this.state.counter === 5) {
            throw new Error('模拟组件崩溃！');
        }
        return <div>
            
            <button onClick={this.handleClick}>+</button>
            {this.state.counter}
            <button onClick={this.decClick}>-</button>
        </div>
    }
}

export default BuggyComponent;